package com.magenta.maxoptra.gis;


import com.magenta.maxoptra.gis.commons.StringUtils;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.Principal;

/*
* Project: MaxOptRA v1.1
* User: Ivan Usalko
* Date: 10.09.2009 16:41:09
*
* Copyright (c) 1999-2009 Magenta Corporation Ltd. All Rights Reserved.
* Magenta Technology proprietary and confidential.
* Use is subject to license terms.
*/

/**
 * Encapsulate provider connection settings.
 */
public class GeoProviderSettings implements Serializable {
    private static final long serialVersionUID = 5495333882707479309L;

    private String providerName;
    /**
     * Version API for geo informational provider connection.
     */
    private String versionApi;

    private URL proxyURL;
    private String proxyLogin;
    private ConnectionCredentials proxyCredentials;
    private GisParameters gisParams;
    private boolean initMemoryCacheEntirely = false;

    private GeoProviderSettings failSafeProvider = null;

    public GeoProviderSettings(String providerName, String versionApi, @Nullable GisParameters gisParams) {
        this.gisParams = gisParams;
        this.versionApi = versionApi;
        this.providerName = providerName;
    }

    public GeoProviderSettings(String providerName, String versionApi, boolean initMemoryCacheEntirely) {
        this.versionApi = versionApi;
        this.providerName = providerName;
        this.initMemoryCacheEntirely = initMemoryCacheEntirely;
    }

    public boolean isInitMemoryCacheEntirely() {
        return initMemoryCacheEntirely;
    }

    public void setProxy(Proxy proxy) throws MalformedURLException {
        if (proxy.isEnable()) {
            proxyLogin = proxy.getUser();
            proxyURL = new URL("http://" + proxy.getHost() + ':' + proxy.getPort());
            proxyCredentials = new Creds(proxy.getPassword());
        }
    }

    public URL getProxyURL() {
        return proxyURL;
    }

    public String getProxyLogin() {
        return proxyLogin;
    }

    public ConnectionCredentials getProxyCredentials() {
        return proxyCredentials;
    }

    public String getVersionApi() {
        return versionApi;
    }

    public String getProviderName() {
        return providerName;
    }

    public GeoProviderSettings getFailSafeProvider() {
        return failSafeProvider;
    }

    public void setFailSafeProvider(GeoProviderSettings failSafeProvider) {
        this.failSafeProvider = failSafeProvider;
    }

    public boolean hasFailSafeProvider() {
        return failSafeProvider != null;
    }

    @Nullable
    public String getGisParam(final String parameter) {
        return gisParams != null ? gisParams.get(parameter) : null;
    }

    @Nullable
    public String getGisParam(final GisParameter parameter) {
        return getGisParam(parameter.name());
    }

    private class Creds implements ConnectionCredentials, Serializable {
        private static final long serialVersionUID = -7058245399186854349L;
        private final String password;

        private Creds(String password) {
            this.password = password;
        }

        public Principal getUserPrincipal() {
            return null;
        }

        public String getPassword() {
            return password;
        }
    }

    @Nullable
    public GisParameters getGisParams() {
        return gisParams;
    }

    public String getCidDiscriminator() {
        final String provider = providerName.toLowerCase();
        final String accountName = getGisParam(GisParameter.accountName);
        final String use_shortest_route = getGisParam(GisParameter.use_shortest_route.withProvider(provider));
        final String routing_profile = getGisParam(GisParameter.routing_profile.withProvider(provider));
        final String default_vehicle_type = getGisParam(GisParameter.default_vehicle_type.withProvider(provider));
        final String restricted_roads = getGisParam(GisParameter.restricted_roads.withProvider(provider));
        final String use_toll_roads = getGisParam(GisParameter.use_toll_roads.withProvider(provider));
        return packDiscriminator(accountName, use_shortest_route, default_vehicle_type, restricted_roads, use_toll_roads, routing_profile);
    }

    public static String packDiscriminator(final String... discriminator) {
        final StringBuilder sb = new StringBuilder();
        for (final String s: discriminator) {
            if (sb.length() > 0) {
                sb.append('|');
            }
            sb.append(StringUtils.isEmpty(s) ? "" : s);
        }
        return sb.toString();
    }
}